<?php


namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\AdminModel;
use Illuminate\Support\Facades\Cookie;

/**
 * Class AdminDao
 * @package App\Dao
 * author:fhc
 * desc:后台用户的Dao
 * created_at: 2020/10/16{
 */
class AdminDao extends BaseDao
{
    /**
     * @param array $params
     * @return \Illuminate\Database\Eloquent\Builder
     * author:fhc
     * desc:后台的构建对象
     * created_at: {2020/10/16}----10:16 上午
     */
    public static function getAdminBuilderQuery($params = []){
        $query = AdminModel::query();

        $query->when(checkIsHasField($params,'id'),function ($query)use ($params){
            $query->where('id',$params['id']);
        });

        $query->when(checkIsHasField($params,'username'),function ($query)use ($params){
            $query->where('username',$params['username']);
        });


        return $query->orderByDesc('created_at');
    }


    /**
     * @param array $params
     * author:fhc
     * desc:登录功能
     * created_at: 2020/10/16{
     * @return mixed
     * @throws SysExceptions
     */
    public static function login($params = []){
        $info = self::getAdminBuilderQuery([
            'username'=>$params['username']
        ])->first()->toArray();
        //账号的信息
        if(!password_verify($params['password'],$info['password'])){
            self::error('密码不正确');
        }
        Cookie::queue('dailian_u_id',$info['id']);//设置缓存
    }

    /**
     * @param array $params
     * author:fhc
     * desc:获取单条信息
     * created_at: {2020/10/16}----10:49 上午
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public static function getOne($params = [])
    {
        return self::getAdminBuilderQuery($params)->first();
    }
}
